<?php

use App\Models\Demand;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use App\Package\Attendance\ScheduleGenerator;

class SchedulesTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
    	$schedules = [];
    	$demands = Demand::with('genre')->where('genre_id', 1)->get();
		$generator = new ScheduleGenerator();
    	foreach ($demands as $demand) {
    		$start_timestamp = $demand->date . ' ' . $demand->genre->start;
    		$end_timestamp = $generator->endTime($demand->genre->start, $demand->genre->end, $demand->date);
    		$schedules[] = [
    			'company_id' => 1,
    			'project_id' => 1,
    			'genre_id' => $demand->genre_id,
    			'demand_id' => $demand->id,
    			'user_id' => 1,
    			'genre_name' => $demand->genre->name,
    			'date' => $demand->date,
    			'start' => $demand->genre->start,
    			'end' => $demand->genre->end,
    			'start_timestamp' => $start_timestamp,
    			'end_timestamp' => $end_timestamp,
    			'worktime' => strtotime($end_timestamp) - strtotime($start_timestamp),
    			'span' => $demand->genre->start > $demand->genre->end,
    			'created_at' => date('Y-m-d H:i:s'),
    			'updated_at' => date('Y-m-d H:i:s'),
			];
		}

		DB::table('schedules')->insert($schedules);
    }
}
